﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter02
{
    public class TypeConversion : ICodeListing
    {
        public string ExplicitCast { get; set; }

        public string ImplicitCast { get; set; }

        public string NotTypeCast { get; set; }

        public void CodeListing()
        {
            ExplicitCastToInt();
            OverflowAnInteger();
            CheckedBlock();
            ImplicitConvrtToLong();
            ImplicitConvrtToLong2();
            StringParse();
            UseConverter();
            UseToString();
            DoubleTryParse();
        }

        private void ExplicitCastToInt()
        {
            var longNumber = 50918309109L;
            var intNumber = (int)longNumber;
            Console.WriteLine(intNumber);
        }

        private void OverflowAnInteger()
        {
            var n = int.MaxValue;
            n += 1;
            Console.WriteLine(n);
        }

        private void CheckedBlock()
        {
            // int.MaxValue equals 2147483647
            unchecked
            {
                OverflowAnInteger();
            }
        }

        private void UncheckedBlock()
        {
            unchecked
            {
                // int.MaxValue equals 2147483647
                var n = int.MaxValue;
                n = n + 1;
                Console.WriteLine(n);
            }
        }

        private void ImplicitConvrtToLong()
        {
            var intNumber = 31416;
            long longNumber = intNumber;
            Console.WriteLine(longNumber);
        }

        private void ImplicitConvrtToLong2()
        {
            var intNumber = 31416;
            long longNumber = (long)intNumber;
            Console.WriteLine(longNumber);
        }

        private void StringParse()
        {
            var text = "9.11E-31";
            var kgElectronMass = float.Parse(text);
            Console.WriteLine(kgElectronMass);
        }

        private void UseConverter()
        {
            var middleConvertText = "278.4378";
            var middle = Convert.ToDouble(middleConvertText);
            var boolean = Convert.ToBoolean(middle);
            Console.WriteLine(boolean);
        }

        private void UseToString()
        {
            var boolean = true;
            var text = boolean.ToString();
            //Dispaly "True"
            Console.WriteLine(text);
        }

        private void DoubleTryParse()
        {
            double number;
            string input;
            Console.WriteLine("Enter a number");
            input = Console.ReadLine();
            if (double.TryParse(input,out number))
            {
                //Converted correctyle, now use number
                //...
            }
            else
            {
                Console.WriteLine("The text entered was not a valid number.");
            }
        }
    }
}
